import request from '@/config/axios'

// 班级数据结构
export interface ClassVO {
  id?: number
  name: string
  description?: string
  teacherId?: number
  teacherName?: string
  memberCount?: number
  status?: number
  createTime?: string
  updateTime?: string
}

// 班级成员数据结构
export interface ClassMemberVO {
  id?: number
  classId: number
  userId: number
  nickname?: string
  mobile?: string
  joinTime?: string
  status?: number
}

// 查询班级列表
export const getClassPage = (params: {
  pageNo?: number
  pageSize?: number
  name?: string
  teacherId?: number
  status?: number
  createTime?: string[]
}) => {
  // 转换参数名以匹配后端接口
  const backendParams = {
    current: params.pageNo || 1,
    size: params.pageSize || 10,
    keyword: params.name,
    status: params.status
  }
  return request.get({ url: '/api/miniadmin/classes', params: backendParams })
}

// 新增班级
export const createClass = (data: ClassVO) => {
  return request.post({ url: '/api/miniadmin/classes', data })
}

// 修改班级
export const updateClass = (data: ClassVO) => {
  return request.put({ url: `/api/miniadmin/classes/${data.id}`, data })
}

// 获取班级详情
export const getClass = (id: number) => {
  return request.get({ url: `/api/miniadmin/classes/${id}` })
}

// 删除班级
export const deleteClass = (id: number) => {
  return request.delete({ url: `/api/miniadmin/classes/${id}` })
}

// 获取班级成员列表
export const getClassMembers = (classId: number, params: {
  pageNo?: number
  pageSize?: number
}) => {
  // 转换参数名以匹配后端接口
  const backendParams = {
    current: params.pageNo || 1,
    size: params.pageSize || 10
  }
  return request.get({ url: `/api/miniadmin/classes/${classId}/members`, params: backendParams })
}

// 添加班级成员
export const addClassMember = (classId: number, userId: number) => {
  return request.post({ url: `/api/miniadmin/classes/${classId}/members`, params: { userId } })
}

// 移除班级成员
export const removeClassMember = (classId: number, userId: number) => {
  return request.delete({ url: `/api/miniadmin/classes/${classId}/members/${userId}` })
}